<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous" />
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
<div class="container-fluid" id="app">
    <div class="container-fluid">
        <div class="row">
            <div class="col-md-12 text-center" style="background-color: red; color: white" >
                <h3>蜗牛书店</h3>
            </div>
        </div>

        <div class="row" style="margin-top: 10px">
            <div class="col-md-6">
                <ol class="breadcrumb">
                    <li v-for="bt in typelist"><a href="javascript:void(0)" :href="'/book/showBookView/?typeid='+bt.id" v-text="bt.name"></a></li>
                </ol>
            </div>

            <div class="col-md-3">
                <form class="form-inline">
                    <div class="form-group">
                        <label for="search" class="sr-only">请输入关键词</label>
                        <input type="text" class="form-control" v-model="searchName" id="search" placeholder="请输入关键词">
                    </div>
                    <button type="button" @click.prevent="search" class="btn btn-default">搜索</button>
                </form>
            </div>

            <div class="col-md-3">
                <span v-if="account == ''">
                        <a class="btn btn-success" th:href="@{/login.html}">登录</a>
                        <a class="btn btn-primary" th:href="@{/reg.html}">注册</a>
                    </span>

                <span v-if="account != ''">
                        欢迎您: {{account}}  &nbsp;&nbsp;
                        <a class="btn btn-primary" href="">退出</a>
                    </span>

                <button type="button" class="btn btn-default btn-sm">个人中心</button>
                <a   class="btn btn-default btn-sm" th:href="@{/cart.html}">我的购物车</a>
                <a  class="btn btn-default btn-sm" th:href="@{/order.html}">我的订单</a>
            </div>
        </div>
    </div>

    <div class="container">

        <div class="row">
            <div class="col-sm-6 col-md-4" v-for="b in bookList">
                <div class="thumbnail">
                    <a :href="'/book/singleBook?bid='+b.id"><img :src="b.imgsrc" alt="图片"></a>
                    <div class="caption text-center">
                        <p>书名:<span v-text="b.name"></span></p>
                        <p>价格:<span  v-text="b.price"></span></p>
                        <!--<span v-text="index"></span>-->
                        <p>
                            <a class="btn btn-primary" :href="'/book/singleBook?bid='+b.id">详情</a>  &nbsp;&nbsp;&nbsp;&nbsp;
                            <a class="btn btn-primary" href="javascript:void(0)" @click="addToCart(b.id)">加入购物车</a>
                        </p>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <a href="javascript:void(0)" @click="go(prePage)" class="btn btn-primary">上一页</a> &nbsp;&nbsp;<a href="javascript:void(0)" @click="go(nextPage)" class="btn btn-primary">下一页</a>
            </div>
        </div>
    </div>

    <div class="container">
        ==========={{typeid}}============
    </div>
</div>

<script th:inline="javascript">
    let vueApp = new Vue({
        el:"#app",
        data:{
            typelist:[],
            bookList:[],
            typeid:[[${typeid}]],
            pageNum:1,
            pageSize:10,
            searchName:'',
            total:0,
            pages:0,
            prePage:1,
            nextPage:1,
            account:''
        },
        methods:{
            initBookType(){
                let _this = this ;
                let url = "../../home/typeList" ;
                $.get(url,function (data){
                    console.log(data);
                    _this.typelist = data ;
                },"json")
            },
            serchBookByType(){
                let url = "../../book/showBook" ;
                let param = "tid="+this.typeid +"&now="+this.pageNum +"&size="+this.pageSize +"&searchName="+this.searchName
                url += "?" + param ;
                let _this = this ;
                $.get(url,function (data){
                    _this.bookList = data.records ;
                    _this.pageNum = data.current ;
                    _this.pageSize = data.size ;
                    _this.total = data.total ;
                    _this.pages = data.pages ;
                    _this.prePage = data.current-1 ;
                    _this.nextPage = data.current+1 ;
                },"json")
            },
            go(num){
                this.pageNum = num;
                //alert(num);
                this.serchBookByType();
            },
            search(){
                this.pageNum = 1;
                this.serchBookByType();
            },
            //获取用户名
            getAccount(){
                let url = "/user/getAccount" ;
                let _this = this ;
                $.get(url,function (data){
                    _this.account = data ;
                })
            },
            addToCart(id){
                //alert(id);
                let url = "/cart/addToCart" ;
                let _this = this ;
                $.post(url,{bookId:id},function (data){
                    if (data == 'ok'){
                        alert("添加成功!")
                    }else {
                        alert("添加失败!");
                    }
                });
            }
        },
        created(){
           // this.typeid = [[${typeid}]];
            this.initBookType();
            this.serchBookByType();
            this.getAccount();
        }
    });
</script>
</body>
</html>